﻿//-----------------------------------------------------------------------
// <copyright file="GoatFieldProperties.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Object
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Class which contains key value pairs which represent properties 
    /// on a field.
    /// </summary>
    public class GoatFieldProperties
    {
        #region Fields - Private

        /// <summary>
        /// Key value pairs
        /// </summary>
        private IDictionary<string, string> values;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the properties object.
        /// </summary>
        /// <param name="values">Key value pairs to wrap.</param>
        public GoatFieldProperties(IDictionary<string, string> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values", "Values must be non-null.");
            }

            this.values = values;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Tests if we have the passed key.
        /// </summary>
        /// <param name="key">Key to test for.</param>
        /// <returns>Returns true if we have the passed key.</returns>
        public bool ContainsKey(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }

            return this.values.ContainsKey(key);
        }

        /// <summary>
        /// Get a string.
        /// </summary>
        /// <param name="name">Name of the string to get.</param>
        /// <param name="defaultValue">Default value for the string if not found.</param>
        /// <returns>Returns the string associated with the passed name or defaultValue if it was not found.</returns>
        public string GetString(string name, string defaultValue = default(string))
        {
            string value;
            if (!this.values.TryGetValue(name, out value))
            {
                return defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Get a bool.
        /// </summary>
        /// <param name="name">Name of the bool to get.</param>
        /// <param name="defaultValue">Default value for the bool if not found.</param>
        /// <returns>Returns the bool associated with the passed name or defaultValue if it was not found.</returns>
        public bool GetBool(string name, bool defaultValue = default(bool))
        {
            string value;
            if (!this.values.TryGetValue(name, out value))
            {
                return defaultValue;
            }

            bool res;
            if (!bool.TryParse(value, out res))
            {
                return defaultValue;
            }

            return res;
        }

        /// <summary>
        /// Get a int.
        /// </summary>
        /// <param name="name">Name of the int to get.</param>
        /// <param name="defaultValue">Default value for the int if not found.</param>
        /// <returns>Returns the int associated with the passed name or defaultValue if it was not found.</returns>
        public int GetInt(string name, int defaultValue = default(int))
        {
            string value;
            if (!this.values.TryGetValue(name, out value))
            {
                return defaultValue;
            }

            int res;
            if (!int.TryParse(value, out res))
            {
                return defaultValue;
            }

            return res;
        }

        /// <summary>
        /// Get a float.
        /// </summary>
        /// <param name="name">Name of the float to get.</param>
        /// <param name="defaultValue">Default value for the float if not found.</param>
        /// <returns>Returns the float associated with the passed name or defaultValue if it was not found.</returns>
        public float GetFloat(string name, float defaultValue = default(float))
        {
            string value;
            if (!this.values.TryGetValue(name, out value))
            {
                return defaultValue;
            }

            float res;
            if (!float.TryParse(value, out res))
            {
                return defaultValue;
            }

            return res;
        }

        #endregion
    }
}
